package com.ncmmall.mall.controller.business.live;

import com.ncmmall.application.business.live.LiveApplication;
import com.ncmmall.application.business.member.MemberApplication;
import com.feijin.commons.exceptions.IllegalRequestException;
import com.feijin.commons.securitys.MD5HashUtils;
import com.feijin.commons.webs.Webs;
import com.ncmmall.domain.security.user.QWebUser;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 直播间管理
 *
 * @author Samuel
 * @date 2020-09-03 11:06
 */
@Controller
@RequestMapping(value = "/live")
@Slf4j
public class LiveController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private LiveApplication liveApplication;

    @Autowired
    private MemberApplication memberApplication;

    /**
     * 直播间列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, QueryPageRequest pageRequest, WebUser params) {

        Map<String, Object> map = new HashMap<>();
        String hql = buildHQL(params, map);

        QueryPage<WebUser> page = queryChannelService.findPage(hql, pageRequest.setParams(map), WebUser.class);
        model.addAttribute("page", page);
        model.addAttribute("params", params);

        return "mall/live/list";
    }

    private String buildHQL(WebUser params, Map<String, Object> map) {

        StringBuilder hql = new StringBuilder("select w from WebUser w where w.liveType = 1");

        if (StringUtils.isNotBlank(params.getLiveID())) {
            hql.append(" and w.liveID like :liveId");
            map.put("liveId", "%" + params.getLiveID() + "%");
        }

        if (StringUtils.isNotBlank(params.getLiveName())) {
            hql.append(" and w.liveName like :lname");
            map.put("lname", "%" + params.getLiveName() + "%");
        }

        if (StringUtils.isNotBlank(params.getLiveIntroduce())) {
            hql.append(" and w.liveIntroduce like :liveIntroduce");
            map.put("liveIntroduce", "%" + params.getLiveIntroduce() + "%");
        }

        if (params.getAccountLocked() == 1) {
            hql.append(" and w.accountNonLocked = true");
        } else if (params.getAccountLocked() == 2) {
            hql.append(" and w.accountNonLocked = false");
        }

        hql.append(" order by w.liveCreateTime desc");
        return hql.toString();
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Model model, long id) {
        WebUser webUser = queryChannelService.findOne(WebUser.class, id);
        List<WebUser> emp = queryChannelService.findAll(QWebUser.webUser.anchor.id.eq(id), WebUser.class);
        emp.add(webUser);
        model.addAttribute("webUser", webUser);
        model.addAttribute("emp", emp);
        return "mall/live/detail";
    }

    /**
     * 修改密码
     */
    @RequestMapping(value = "/modifyAssistantPassword", method = RequestMethod.GET)
    private String modifyPassword(Model model, long id) {
        WebUser webUser = queryChannelService.findOne(WebUser.class, id);
        model.addAttribute("webUser", webUser);
        return "mall/live/modifyPassword";
    }

    /**
     * 修改助理登录密码
     */
    @RequestMapping(value = "/modifyAssistantPassword", method = RequestMethod.PUT)
    @ResponseBody
    private Map<String, Object> modifyAssistantPassword(HttpServletRequest request, WebUser params) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalRequestException("request must be ajax !");
            }
            WebUser user = queryChannelService.findOne(WebUser.class, params.getId());
            memberApplication.modifyPassword(user, MD5HashUtils.MD5(params.getPassword()));
            return ImmutableMap.of("status", true);
        } catch (Exception e) {
            log.error("[修改密码异常]", e);
            return ImmutableMap.of("status", false, "message", "修改异常");
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> remove(HttpServletRequest request, WebUser params) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            liveApplication.removeAchor(params);
            return ImmutableMap.of("status", true, "message", "删除成功");
        } catch (Exception e) {
            log.error("账号删除异常", e);
            return ImmutableMap.of("status", false, "message", "删除异常");
        }
    }
}
